package com.alinu.workbench.web.controller;

import com.alinu.settings.domain.User;
import com.alinu.utils.DateTimeUtil;
import com.alinu.utils.PrintJson;
import com.alinu.utils.ServiceFactory;
import com.alinu.utils.UUIDUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.Activity;
import com.alinu.workbench.domain.Contacts;
import com.alinu.workbench.domain.Tran;
import com.alinu.workbench.domain.TranHistory;
import com.alinu.workbench.service.ActivityService;
import com.alinu.workbench.service.ContactsService;
import com.alinu.workbench.service.CustomerService;
import com.alinu.workbench.service.TranService;
import com.alinu.workbench.service.impl.ActivityServiceImpl;
import com.alinu.workbench.service.impl.ContactsServiceImpl;
import com.alinu.workbench.service.impl.CustomerServiceImpl;
import com.alinu.workbench.service.impl.TranServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/21
 * @description 交易控制器类
 */
public class TransactionController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        assert path != null;
        switch (path) {
            case "/workbench/transaction/getTransList.do":
                getTransList(request, response);
                break;
            case "/workbench/transaction/getCustomerName.do":
                getCustomerName(request, response);
                break;
            case "/workbench/transaction/createTransaction.do":
                createTransaction(request, response);
                break;
            case "/workbench/transaction/getActivitiesByName.do":
                getActivitiesByName(request, response);
                break;
            case "/workbench/transaction/getContactsByName.do":
                getContactsByName(request, response);
                break;
            case "/workbench/transaction/detail.do":
                detail(request, response);
                break;
            case "/workbench/transaction/getTranHistoryList.do":
                getTranHistoryList(request, response);
                break;
            case "/workbench/transaction/changeStage.do":
                changeStage(request, response);
                break;
            default:
                break;
        }

    }

    private void changeStage(HttpServletRequest request, HttpServletResponse response) {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());

        String id = request.getParameter("id");
        String stage = request.getParameter("stage");
        String money = request.getParameter("money");
        String expectedDate = request.getParameter("expectedDate");
        String editBy = ((User) request.getSession().getAttribute("user")).getName();

        Map<String, String> stagePossibility = (Map<String, String>) this.getServletContext().getAttribute("stagePossibility");
        String possibility = stagePossibility.get(stage);
        Tran tran = new Tran();
        tran.setEditTime(DateTimeUtil.getSysTime());
        tran.setEditBy(editBy);
        tran.setMoney(money);
        tran.setExpectedDate(expectedDate);
        tran.setId(id);
        tran.setStage(stage);
        tran.setPossibility(possibility);

        boolean result = tranService.changeStage(tran);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result", result);
        resultMap.put("tran", tran);

        PrintJson.printJsonObj(response, resultMap);

    }

    private void getTranHistoryList(HttpServletRequest request, HttpServletResponse response) {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());

        String tranId = request.getParameter("tranId");

        List<TranHistory> tranHistoryList = tranService.getTranHistoryList(tranId);

        PrintJson.printJsonObj(response, tranHistoryList);

    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());
        String id = request.getParameter("id");

        Tran tran = tranService.findTranById(id);

        String stage = tran.getStage();

        Map<String, String> stagePossibility = (Map<String, String>) this.getServletContext().getAttribute("stagePossibility");
        String possibility = stagePossibility.get(stage);

        tran.setPossibility(possibility);
        request.setAttribute("tran", tran);
        request.getRequestDispatcher("/workbench/transaction/detail.jsp").forward(request, response);

    }

    private void getContactsByName(HttpServletRequest request, HttpServletResponse response) {
        ContactsService contactsService = (ContactsService) ServiceFactory.getService(new ContactsServiceImpl());

        String fullname = request.getParameter("fullname");

        List<Contacts> contactsList = contactsService.findContactsByName(fullname);

        PrintJson.printJsonObj(response, contactsList);

    }

    private void getActivitiesByName(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String name = request.getParameter("name");

        List<Activity> activityList = activityService.findActivityByName(name);

        PrintJson.printJsonObj(response, activityList);

    }

    private void createTransaction(HttpServletRequest request, HttpServletResponse response) throws IOException {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());

        String owner = request.getParameter("owner");
        String money = request.getParameter("money");
        String name = request.getParameter("name");
        String expectedDate = request.getParameter("expectedDate");
        String stage = request.getParameter("stage");
        String type = request.getParameter("type");
        String activityId = request.getParameter("activityId");
        String source = request.getParameter("source");
        String contactsId = request.getParameter("contactsId");
        String description = request.getParameter("description");
        String contactsSummary = request.getParameter("contactsSummary");
        String createBy = ((User) request.getSession().getAttribute("user")).getName();
        String nextContactTime = request.getParameter("nextContactTime");
        String customerName = request.getParameter("customerName");
        String possibility = request.getParameter("possibility");

        Tran tran = new Tran();
        tran.setType(type);
        tran.setOwner(owner);
        tran.setNextContactTime(nextContactTime);
        tran.setDescription(description);
        tran.setContactSummary(contactsSummary);
        tran.setContactsId(contactsId);
        tran.setActivityId(activityId);
        tran.setExpectedDate(expectedDate);
        tran.setId(UUIDUtil.getUUID());
        tran.setCreateBy(createBy);
        tran.setCreateTime(DateTimeUtil.getSysTime());
        tran.setMoney(money);
        tran.setName(name);
        tran.setStage(stage);
        tran.setSource(source);
        tran.setPossibility(possibility);

        boolean result = tranService.createTransaction(tran, customerName);

        if (result) {
            response.sendRedirect(request.getContextPath() + "/workbench/transaction/index.jsp");
        } else {
            System.out.println("创建交易失败");
        }


    }

    private void getCustomerName(HttpServletRequest request, HttpServletResponse response) {
        CustomerService customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        String name = request.getParameter("name");

        List<String> customerNameList = customerService.getCustomerName(name);

        PrintJson.printJsonObj(response, customerNameList);

    }

    private void getTransList(HttpServletRequest request, HttpServletResponse response) {
        TranService tranService = (TranService) ServiceFactory.getService(new TranServiceImpl());

        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        int start = (pageNo - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        params.put("start", start);
        params.put("pageSize", pageSize);
        params.put("owner", request.getParameter("owner"));
        params.put("name", request.getParameter("name"));
        params.put("customerName", request.getParameter("customerName"));
        params.put("stage", request.getParameter("stage"));
        params.put("type", request.getParameter("type"));
        params.put("source", request.getParameter("source"));
        params.put("contactsName", request.getParameter("contactsName"));
        PaginationVO<Tran> dataList = tranService.getTransList(params);

        PrintJson.printJsonObj(response, dataList);

    }
}
